<?php

namespace app\admin\model;

use think\Model;
use traits\model\SoftDelete;

use app\admin\model\Questions;

class Testpaper extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'testpaper';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'diffdata_text',
        'questionslist',
        'status_text'
    ];
    

    
    public function getDiffdataList()
    {
        return ['0' => __('Diffdata 0'), '1' => __('Diffdata 1'), '2' => __('Diffdata 2'), '3' => __('Diffdata 3'), '4' => __('Diffdata 4'), '5' => __('Diffdata 5')];
    }

    public function getStatusList()
    {
        return ['1' => __('Status 1'), '2' => __('Status 2'), '3' => __('Status 3')];
    }


    public function getDiffdataTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['diffdata']) ? $data['diffdata'] : '');
        $list = $this->getDiffdataList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }




    public function course()
    {
        return $this->belongsTo('Course', 'course_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function admin()
    {
        return $this->belongsTo('Admin', 'admin_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function getQuestionslistAttr($value,$data)
    {
      $questions = json_decode($data["questions"],true);

      $list = [];
      foreach ($questions as $key => $value) {
        # code...
        $ids = array_column($value,"id");
        if(empty($ids)){
            $list[$key] = [];
        }
        else{
            // "1,2,3,4"
            $ids_str = implode(',',$ids);
            $list[$key] = (new Questions)->where('id','in',$ids)->orderRaw("FIELD(id,{$ids_str})")->select();
        }
      }

      return json_encode($list);
    }

    public function getMaoshutestAttr()
    {
      return 1234;
    }

    // 根据问题id删除一条题目
    public function delQuestion($question_id)
    {
        $paperQuesArray = $this->where('questions','like','%"id":'.$question_id.',%')->column('questions','id');
        if($paperQuesArray){
            // var_dump($paperQuesArray);
            foreach ($paperQuesArray as $id => $question) {
                # code...
                $questionArr = json_decode($question,true);

                $newTypeArr = [];
                foreach ($questionArr as $key => $arr) {
                    $newArr = array_filter($arr,function($value) use ($question_id){
                        return $value["id"] != $question_id;
                    });
                    $newTypeArr[$key] = array_values($newArr);
                }

                $this->where('id',$id)->update([
                    "questions"=>json_encode($newTypeArr)
                ]);
            }
        }
    }
    
}
